<?php 


//数据缓存工具类
class MemcacheUtil{

	private $mem_connect;
	private $mem_port;
	private $mem_compressed;
	private $mem_life_time;


	public function __construct(){

		$conf= include dirname(__FILE__)."/../conf/conf.php";

		//如果没有开启缓存服务,抛出异常
		if(!$conf['memcache_open']){
			throw new Exception("memcache is  closed", 500);			
		}

		$this->mem_connect=$conf["mem_connect"];
		$this->mem_port=$conf["mem_port"];
		$this->mem_compressed=$conf["mem_compressed"];
		$this->mem_life_time=$conf["mem_life_time"];
	}


	public function save($key,$data,$life_time=0){

		$mem=new Memcache;

		$mem->connect($this->mem_connect,$this->mem_port);

		if($life_time==0) $life_time=$this->mem_life_time;

		$mem->set($key,$data,$this->mem_compressed,$life_time);

		$mem->close();

	}


	public function get($key){

		$mem=new Memcache;

		$mem->connect($this->mem_connect,$this->mem_port);

		$data=$mem->get($key);

		$mem->close();

		return $data;
	}


}


